package com.sh.personnel.security.config;

import com.alibaba.fastjson.JSONObject;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.util.HashMap;

/**
 * @author 林思浩
 * @date 2020/03/02 17:54
 */
public class CustomRequestWrapper extends HttpServletRequestWrapper {

    private final String body;

    public CustomRequestWrapper(HttpServletRequest request) {
        super(request);
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            if(inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] chars = new char[128];
                int tempRead = -1;
                while((tempRead = bufferedReader.read(chars)) > 0) {
                    stringBuilder.append(chars, 0, tempRead);
                }
            }else {
                stringBuilder.append("");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        body = stringBuilder.toString();
    }

    /**
     * 使用@RequestBody注解的时候，其实框架是调用了getInputStream()方法，所以我们要重写这个方法
     * 作用：让输入流变成读的是body参数
     * @return
     * @throws IOException
     */
    @Override
    public ServletInputStream getInputStream() throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
    }

    /**
     * 作用：将输入流转换并封装进缓存字符输入流中
     * @return
     * @throws IOException
     */
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    /**
     * 获取POST请求体的所有参数
     * @return
     */
    public String getBody() {
        return body;
    }

    /**
     * 获取邮箱号email和验证码code
     * @return
     */
    public HashMap getMailCode() {
        JSONObject jsonObject = JSONObject.parseObject(body);
        HashMap map = new HashMap ();
        map.put("email", jsonObject.get("email"));
        map.put("code", jsonObject.get("code"));
        return map;
    }

}
